<template>
  <div class="app-container org">
    <el-card>
      <el-row>
        <el-col :span="24">
          <p class="pinformation">搜索条件</p>
        </el-col>
      </el-row>
      <el-form ref="queryRef" label-width="100" :model="queryParams">
        <el-row>
          <el-col :span="3">
            <el-form-item label="自制样品名称：" prop="sampleName">
              <el-input clearable maxlength="100" placeholder="请输入自制样品名称" v-model="queryParams.sampleName"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="唯一性编号：" prop="sampleCode">
              <el-input clearable maxlength="100" placeholder="请输入唯一性编号" v-model="queryParams.sampleCode"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-form-item label="配置人：" prop="selfAdminId" label-width="65">
              <el-select v-model="queryParams.selfAdminId" clearable filterable>
                <el-option v-for="item in proxy.addListNoLimit(userList, 'userId', 'nickName')" :key="item.userId"
                  :value="item.userId" :label="item.nickName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="配置日期：" prop="" label-width="80">
              <el-date-picker value-format="YYYY-MM-DD" type="daterange" v-model="dateRange" range-separator="-"
                start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="检测项目：" prop="bucketId" label-width="80">
              <el-select v-model="queryParams.bucketId" clearable filterable remote reserve-keyword remote-show-suffix
                :remote-method="remoteMethod" :loading="loading" style="width: 100%">
                <el-option v-for="item in jcxmList" :key="item.bucketId" :value="item.bucketId" :label="item.jcxmName">
                  <span v-html="transformation(item.jcxmName)"></span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="检测项目类型：" prop="isSingle">
              <el-select placeholder="请选择" clearable v-model="queryParams.isSingle" filterable>
                <el-option v-for="dict in proxy.addListNoLimit(sys_bzwz_jcxm_type)" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="3" style="padding-left: 30px;">
            <el-row>
              <el-radio label="0" v-model="queryParams.isLike">模糊搜索</el-radio>
              <el-radio label="1" v-model="queryParams.isLike">精准搜索</el-radio>
            </el-row>
          </el-col>
          <el-col :span="2">
            <el-row>
              <el-button type="success" @click="handleQuery">搜索</el-button>
              <el-button class="resetBtn" @click="resetQuery">重置</el-button>
            </el-row>
          </el-col>
        </el-row>
      </el-form>
      <el-row>
        <p class="pinformation">待处置物质列表</p>
        <p class="totalStyle">共 {{ total }} 条数据</p>
        <el-row style="padding-bottom: 5px; margin-left: 20px;">
          <el-button type="success" link icon="Plus" @click="bztchDue" :disabled="!sampleIds || sampleIds.length == 0">批量处置</el-button>
        </el-row>
        <el-table ref="mainTableRef" :data="overdueTable" @selection-change="dueHandleSelChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="唯一性编号" prop="sampleCode" align="center" />
          <el-table-column label="自制样品名称" prop="sampleName" align="center" />
          <el-table-column label="种类" prop="sampleType" align="center">
            <template #default="scope">
              <dict-tag :options="sys_self_sample_type" :value="scope.row.sampleType" />
            </template>
          </el-table-column>
          <el-table-column label="溶剂" prop="rj" align="center">
          </el-table-column>
          <el-table-column label="定容体积" prop="drtj" align="center">
            <template #default="scope">
              <span v-html="scope.row.drtj + transformation(scope.row.drtjUnitName)"></span>
            </template>
          </el-table-column>
          <el-table-column label="配置日期" prop="createDate" align="center" />
          <el-table-column label="有效日期" prop="yxDate" align="center" />
          <el-table-column label="配置人" prop="selfAdminId" align="center">
            <template #default="scope">
              {{
                proxy.getItemByArray(
                  userList,
                  "userId",
                  scope.row.selfAdminId,
                  "nickName"
                )
              }}
            </template>
          </el-table-column>
          <el-table-column label="检测项目数" prop="jcxmCount" align="center">
            <template #default="scope">
              <el-button link @click="jcxmCountClick(scope.row.sampleId)">{{
                scope.row.jcxmName ? scope.row.jcxmName : scope.row.jcxmCount
              }}</el-button>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-button link type="success" @click="useBzwz(scope.row.sampleId)">处置</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-row>
    </el-card>
    <!-- 点击行内检测项目数的弹窗 -->
    <el-dialog :class="'el-dialog-theme3'" append-to-body draggable :close-on-click-modal="false"
      v-model="jcxmOpen" @close="closedDialog" :title="title">
      <el-row>
        <p class="pinformation">检测项目列表</p>
        <p class="totalStyle">共 {{ multiplexTable.length }} 条数据</p>
      </el-row>
      <el-table :data="multiplexTable">
        <el-table-column label="检测项目" prop="bucketId" align="center">
          <template #default="scope">
            <span v-html="transformation(scope.row.jcxmName)"></span>
          </template>
        </el-table-column>
        <el-table-column label="浓度值" prop="con" align="center" />
        <el-table-column label="浓度值单位" prop="conUnit" align="center">
          <template #default="scope">
            <span v-html="transformation(scope.row.conUnitName)"></span>
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="danger" @click="jcxmOpen = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 点击领用弹窗 -->
    <el-dialog :class="'el-dialog-theme3'" append-to-body draggable :close-on-click-modal="false"
      v-model="useOpen" @close="closedDialog" :title="useTitle" width="300">
      <el-form :model="lyForm">
        <el-form-item label="已选中标样编号数：" prop="">{{
          lyForm.codeCount
        }}</el-form-item>
        <el-form-item label="已选中检测项目数：" prop="">{{
          lyForm.jcxmCount
        }}</el-form-item>
        <el-form-item label="处置人：" prop="selfHandleId" label-width="80">
          <el-select v-model="lyForm.selfHandleId" placeholder="请选择处置人">
            <el-option v-for="item in userList" :key="item.userId" :label="item.nickName" :value="item.userId" />
          </el-select>
        </el-form-item>
        <el-form-item label="处置方式：" prop="" label-width="80">
          <el-select placeholder="请选择" v-model="lyForm.handleType" filterable>
            <el-option v-for="dict in sys_bzwz_handle_type" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)" />
          </el-select>
        </el-form-item>
        <el-form-item label="处置日期：" prop="" label-width="80">
          <el-date-picker placeholder="请选择处置日期" type="date" v-model="lyForm.handleDate" value-format="YYYY-MM-DD"
            :clearable="false" :editable="false" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="success" @click="save">保 存</el-button>
          <el-button type="danger" @click="useOpen = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="BzwzOutin">
import {
  sampleList,
  listDelayHandle,
  handleForm,
  saveBatchHandle,
  getJcxmList
} from "@/api/resource/selfSampleLedger";
import { listUser } from "@/api/contract/check";
import { getUnitList } from "@/api/store/unit";
import { getCurrentTime, getCurrentUser } from "@/api/common";

const { proxy } = getCurrentInstance();
const {
  sys_self_sample_type,
  sys_bzwz_gslb,
  sys_bzwz_gqlx,
  sys_bzwz_jcxm_type,
  sys_bzwz_handle_type,
} = proxy.useDict(
  "sys_self_sample_type",
  "sys_bzwz_gslb",
  "sys_bzwz_gqlx",
  "sys_bzwz_jcxm_type",
  "sys_bzwz_handle_type"
);

const overdueTable = ref([]);
const multiplexTable = ref([]);
const dateRange = ref([]);
const jcxmList = ref([]);
const title = ref("查看检测项目");
const jcxmOpen = ref(false);
const useOpen = ref(false);
const useTitle = ref("自制样品超期处置");
const data = reactive({
  queryParams: {
    pageNum: 1, // 当前页数
    pageSize: 10, // 当前页数据条数
    isLike: "0",
    selfAdminId: -1,
    isSingle: -1
  },
});

const { queryParams } = toRefs(data);

const heightParams = ref([{ tableName: "main", bottomHeight: 21 }]);

function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}
onMounted(() => {
  nextTick(() => {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  })
})
onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

const total = ref(0);
function getList() {
  listDelayHandle(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (res) => {
      overdueTable.value = res.rows;
      total.value = res.total;
    }
  );
}

getList();

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  queryParams.value.isLike = "0"
  handleQuery();
}

function jcxmCountClick(row) {
  sampleList({ sampleId: row }).then((res) => {
    multiplexTable.value = res.data;
    jcxmOpen.value = true;
  });
}

function closedDialog() {
  multiplexTable.value = [];
}

const unitList = ref([]);
getUnitList({ unitGroup: 0 }).then((res) => {
  unitList.value = res.data;
});

const lyForm = ref({});

function useBzwz(id) {
  handleForm([id]).then((res) => {
    lyForm.value = res.data;
    getCurrentUser().then((res) => {
      lyForm.value.selfHandleId = res.data;
    });
    getCurrentTime().then((res) => {
      lyForm.value.handleDate = res.data;
    });
    lyForm.value.handleType = 0;
  });
  useOpen.value = true;
}

function save() {
  if (lyForm.value.sampleId) {
    lyForm.value.sampleIds = [lyForm.value.sampleId];
    delete lyForm.value.sampleId;
  } else {
    lyForm.value.sampleIds = sampleIds.value;
  }
  delete lyForm.value.codeCount;
  delete lyForm.value.jcxmCount;
  saveBatchHandle(lyForm.value).then((res) => {
    proxy.$modal.msgSuccess(res.msg);
    useOpen.value = false;
    getList();
  });
}

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "main", bottomHeight: 73 }];
    } else {
      heightParams.value = [{ tableName: "main", bottomHeight: 21 }];
    }
    getTableHeight();
  }
);

function transformation(row) {
  const a = proxy.getSupAndSub(row);
  return a;
}

const sampleIds = ref([]);
function dueHandleSelChange(selection) {
  sampleIds.value = selection.map((row) => {
    return row.sampleId;
  });
}

function bztchDue() {
  handleForm(sampleIds.value).then((res) => {
    lyForm.value = res.data;
    useOpen.value = true;
  });
}

const userList = ref([]);
listUser().then((res) => {
  userList.value = res.data;
});


const loading = ref(false);

function remoteMethod(val) {
  if (val) {
    loading.value = true;
    setTimeout(() => {
      getJcxmList({ jcxmName: val }).then((res) => {
        loading.value = false;
        jcxmList.value = res.rows;
      });
    }, 200);
  } else {
    jcxmList.value = [];
  }
}
</script>

<style lang="scss" scoped>
.el-radio {
  margin-right: 5px;
}
</style>
